package com.morizhang.usersys.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.usersys.api.ApiResult;
import com.morizhang.usersys.api.IService;
import com.morizhang.usersys.api.bean.ErrorDefinition;
import com.morizhang.usersys.api.bean.ErrorDefinitionHolder;
import com.morizhang.usersys.api.bean.UserSysException;
import com.morizhang.usersys.api.dao.BasicUserInfoDao;
import com.morizhang.usersys.api.model.UserModel;
import com.morizhang.usersys.api.service.QueryUserInfoService;
import com.morizhang.usersys.api.service.ServiceAnnotation;
import com.morizhang.usersys.api.utils.ErrorUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;


@ServiceAnnotation(name = "QueryUserInfo")
public class QueryUserInfoServiceImpl implements QueryUserInfoService, IService {
    @Autowired
    protected BasicUserInfoDao basicUserInfoDao;

    protected UserSysException userSysException;

    @Override
    public JSONArray selectUserByUserID(String userID) throws Exception{
        UserModel user = basicUserInfoDao.selectUserByUserID(userID);
        if (user == null) {
            ErrorDefinition error = ErrorDefinitionHolder.get("NO_SUCH_USER");
            userSysException = ErrorUtils.convertErrorToException(error);
            throw userSysException;
        }
        return getUserJsonForm(user);
    }

    @Override
    public JSONArray selectUserByUserName(String userName) throws Exception {
        UserModel user = basicUserInfoDao.selectUserByUserName(userName);
        if (user == null) {
            ErrorDefinition error = ErrorDefinitionHolder.get("NO_SUCH_USER");
            userSysException = ErrorUtils.convertErrorToException(error);
            throw userSysException;
        }
        return getUserJsonForm(user);
    }

    private JSONArray getUserJsonForm(UserModel user) {
        JSONArray result = new JSONArray();
        JSONObject userInfo = new JSONObject();
        userInfo.put("UserID",user.getUserID());
        userInfo.put("UserName",user.getUserName());
        userInfo.put("password",user.getPassword());
        result.add(userInfo);
        return result;
    }

    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        ApiResult result = new ApiResult();
        try {
            JSONObject requestParamsData = requestParams.getJSONObject("data");
            switch (requestParams.getString("requestMethod")) {
                case "selectUserByUserID" -> {
                    String userID = requestParamsData.getString("userID");
                    if (userID == null||userID.length() == 0) {
                        ErrorDefinition error = ErrorDefinitionHolder.get("NO_QUERY_USER_ID");
                        userSysException = ErrorUtils.convertErrorToException(error);
                        throw userSysException;
                    }
                    result.setData(selectUserByUserID(userID));
                    result.setSuccess(true);
                }
                case "selectUserByUserName" -> {
                    String userName = requestParamsData.getString("userName");
                    if (userName == null||userName.length() == 0) {
                        ErrorDefinition error = ErrorDefinitionHolder.get("NO_QUERY_USER_NAME");
                        userSysException = ErrorUtils.convertErrorToException(error);
                        throw userSysException;
                    }
                    result.setData(selectUserByUserName(userName));
                    result.setSuccess(true);
                }
                default -> {
                    ErrorDefinition error = ErrorDefinitionHolder.get("NO_SUCH_QUERY_METHOD");
                    userSysException = ErrorUtils.convertErrorToException(error);
                    throw userSysException;
                }
            }
            return result;
        } catch (UserSysException e) {
            result.setSuccess(false);
            result.setFaultMessage(e.getExceptionMessage());
            result.setException(e);
            return result;
        }
    }
}
